﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using SimpleHttpServer.GuestBook;

namespace SimpleHttpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();

            int port = Settings.Port;

            listener.Prefixes.Add(string.Format("http://*:{0}/", port));
            listener.Start();
            Console.WriteLine(string.Format("Listening on port {0}...", port));
            
            for(;;)
            {
                HttpListenerContext ctx = listener.GetContext();

                Console.WriteLine(ctx.Request.HttpMethod + " " + ctx.Request.Url.AbsoluteUri);

                IGuestBook guestBook;



                if (Settings.PersistanceMode == PersistanceMode.Xml) guestBook = new XmlGuestBook();
                else
                    if (Settings.PersistanceMode == PersistanceMode.SqLite) guestBook = new SqlLiteGuestBook();
                        else throw new Exception("Unsupported persistance provider!");


                new Thread(new Worker(ctx, guestBook).ProcessRequest).Start();
            }
        }
    }
}
